/*
*right now this class isnt completly done
* but it works somewhat i hope
 */
package commands;

import edu.wpi.first.wpilibj.AnalogChannel;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import framework.HW;


    // Called repeatedly when this Command is scheduled to run
/**
 *
 * @author hunter
 */
public class CountBalls extends CommandBase {
    private AnalogChannel ir_sensor1;
    private int ballcount;
    public CountBalls() {
        // Use requires() here to declare subsystem dependencies
        // eg. requires(chassis);
        ir_sensor1=new AnalogChannel(HW.ir_sensor_channel);
        ballcount=0;
    }

    // Called just before this Command runs the first time
    protected void initialize() {
        
    }
    protected void execute() {
        if(ir_sensor1.getVoltage()>2.00)
          waitForFinish();
         SmartDashboard.putInt("number of balls",ballcount);
        
    }

    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {
        return false;
    }

    // Called once after isFinished returns true
    protected void end() {
    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
        SmartDashboard.putString("it dun goofed","ball count failed");
    }
    private void waitForFinish(){
        double lastVoltage=0;
        double currentVoltage=0;
        for(;;){
            lastVoltage=currentVoltage;
            currentVoltage=ir_sensor1.getVoltage();
            if(currentVoltage<lastVoltage-0.02){
                //stopthemoder upperBelt,lowerBelt
                 ballcount++;
                 break;
            }
        }
    }
    public void ballShot(){
        ballcount--;
    }
}
